﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Battle
{
    public abstract class BuffHandlerBase<T> : IBuffHandler where T :class
    {
        public enum EM_BuffAddType
        {
            Normal,
            RefrshTime,
            Merge,
            OnlyOne,
        }

        public string BuffName { get; private set; }
        public EM_BuffAddType BuffAddType { get; private set; }

        public BuffHandlerBase(string name,EM_BuffAddType buffAddType=EM_BuffAddType.Normal)
        {
            BuffName = name;
            BuffAddType = buffAddType;
        }

        public bool TryAddBuff(IBuffMgr buffMgr, ST_ObjBuffData buffData)
        {
            if (buffMgr != null && buffMgr.GetObj<T>() != null)
            {
                var buffs = buffMgr.GetBuffDatas(BuffName);
                switch (BuffAddType)
                {
                    case EM_BuffAddType.RefrshTime:
                        if (buffs.Length > 0)
                        {
                            //已经有了，刷新时间
                            buffs[0].fBornTime = Time.time;
                            buffMgr.ChangeBuff(buffs[0]);
                            return false;
                        }
                        break;
                    case EM_BuffAddType.Merge:
                        if (buffs.Length > 0)
                        {
                            //已经有了，合并
                            buffMgr.ChangeBuff(MergeBuff(buffs[0], buffData));
                            return false;
                        }
                        break;
                    case EM_BuffAddType.OnlyOne:
                        if (buffs.Length > 0)
                        {
                            //已经有了，替换
                            buffData.strGUID = buffs[0].strGUID;
                            buffMgr.ChangeBuff(buffData);
                            return false;
                        }
                        break;
                    default:
                        break;
                }


                return true;
            }
            else
                return false;
        }

        public virtual void OnBuffStart(IBuffMgr buffMgr, ST_ObjBuffData buffData)
        {
        }

        public abstract string[] OnUpdate(IBuffMgr buffMgr, ST_ObjBuffData[] buffDatas, float deltaTime);

        public virtual void OnBuffEnd(IBuffMgr buffMgr, ST_ObjBuffData buffData)
        {
        }

        public virtual ST_ObjBuffData MergeBuff(ST_ObjBuffData one, ST_ObjBuffData other)
        {
            one.fNum += other.fNum;
            one.nNum += other.nNum;

            if (one.dicCustom != null && other.dicCustom != null)
                foreach (var item in other.dicCustom)
                {
                    if (one.dicCustom.ContainsKey(item.Key))
                        one.dicCustom[item.Key] += item.Value;
                }

            return one;
        }
    }
}
